package ballena.activities;

import java.io.File;
import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import ballena.utiles.Global;

public class Imagenes extends Activity {
	private static int TAKE_PICTURE = 1;
	private Uri outputFileUri;

	// private byte[] img;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.imagenes);

		Button btVerFotos = (Button) findViewById(R.id.btVerFoto);
		btVerFotos.setOnClickListener(listenerVerFotos);

		Button btSacarFoto = (Button) findViewById(R.id.btSacarFoto);
		btSacarFoto.setOnClickListener(listenerSacarFoto);

	}

	View.OnClickListener listenerVerFotos = new OnClickListener() {
		public void onClick(View v) {
			Intent aGaleria = new Intent(Imagenes.this, Galeria.class);
			aGaleria.putExtra("id", Global.usuario.getId());
			startActivity(aGaleria);
		}
	};

	View.OnClickListener listenerSacarFoto = new OnClickListener() {
		public void onClick(View v) {
			// Intent aTomarFoto = (new Intent(Imagenes.this, TomarFoto.class));
			// startActivity(aTomarFoto);
			saveFullImage();
		}
	};

	View.OnClickListener listenerSubirFoto = new OnClickListener() {
		public void onClick(View v) {
		}
	};

	private void getThumbailPicture() {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(intent, TAKE_PICTURE);
	}

	private void saveFullImage() {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		File file = new File(Environment.getExternalStorageDirectory(),
				"test.jpg");
		outputFileUri = Uri.fromFile(file);
		Global.ruta_img = outputFileUri.getPath();
		intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
		startActivityForResult(intent, TAKE_PICTURE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		try {
			File f = new File(Global.ruta_img);
			
			// Subimos imagen usando el metodo post
			HttpClient httpClient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(Global.SERVLET+"?accion="+Global.IMAGENES_SUBIR+"&id="+Global.usuario.getId());
			MultipartEntity entity = new MultipartEntity();
			//entity.addPart("imagen", new InputStreamBody(fis, "imagen"));
			entity.addPart("imagen", new FileBody(f));
			httpPost.setEntity(entity);
			HttpResponse servletResponse = httpClient.execute(httpPost);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
